package edu.hawaii.webspider;

import static edu.hawaii.webspider.TestWebIterator.local;
import static edu.hawaii.webspider.TestWebIterator.serverResources;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Map;
import org.junit.Test;
import com.meterware.pseudoserver.PseudoServer;

/**
 * Test suite for WebSpider.
 * 
 * @author Brian Jaress
 * 
 */
public class TestWebSpider {

  /**
   * Test the main method.
   */
  @Test
  public void testMain() {
    try {
      WebSpider.main(new String[] { "-totallinks",
          "http://www.hackystat.org", "10" });
      WebSpider.main(new String[] { "-mostpopular",
          "http://www.hackystat.org", "10"});
    }
    catch (Exception e) {
      fail("Testing main: " + e.toString());
    }
  }

  /**
   * Tests the totalLinks method.
   */
  @Test
  public void testTotalLinks() {
    PseudoServer server = serverResources(new int[][] {
        new int[] { 0, 1, 2 },
        new int[] { 0 },
        new int[] { 1, 1, 1 }
        });
    int links = 0;
    try {
      links = new WebSpider().totalLinks(local(server, "0"), 10);
    }
    catch (Exception e) {
      fail(e.toString());
    }
    assertEquals("total links", 7, links);
    try {
      links = new WebSpider().totalLinks(local(server, "0"), 1);
    }
    catch (Exception e) {
      fail(e.toString());
    }
    assertEquals("total links", 3, links);
  }

  /**
   * Tests the mostPopular method.
   */
  @Test
  public void testMostPopular() {
    PseudoServer server = serverResources(new int[][] {
       new int[] {0, 1},
       new int[] {0, 2},
       new int[] {3, 4, 3, 3, 3, 3, 3},
       new int[] {4},
       new int[] {4}
    });
    Map.Entry<String, Integer> winner = null;
    try {
      winner = new WebSpider().mostPopular(local(server, "0"), 10);
    }
    catch (Exception e) {
      fail(e.toString());
    }
    assertEquals("most popular votes", 3, winner.getValue().intValue());
    try {
      winner = new WebSpider().mostPopular(local(server, "0"), 3);
    }
    catch (Exception e) {
      fail(e.toString());
    }
    assertEquals("most popular", 2, winner.getValue().intValue());
  }
}
